
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.AbstractButton;
import javax.swing.JButton;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import org.jdesktop.animation.timing.Animator;

import org.jdesktop.animation.timing.TimingTarget;
import org.jdesktop.swingx.graphics.GraphicsUtilities;
import org.jdesktop.swingx.image.GaussianBlurFilter;

/**
 *
 * @author Dark_Wolf
 */
public class DarkButton extends JButton{
     private float ghostValue;
     private RoundRectangle2DHighlight rr2d=null;
     private RoundRectangle2D button;
      
     
     public DarkButton(String text,int width, int height) {
        setSize(width, height);
        setText(text);
        setBorderPainted(false);
        setFocusable(false);
        setOpaque(false);
        setBackground(Color.BLUE);//Debug only
        setFont(new Font("Dialog", Font.PLAIN, 10));
        setForeground(Color.WHITE);
        addMouseListener(new HiglightHandler());
        setContentAreaFilled(false);
        
       setVerticalTextPosition(AbstractButton.CENTER);
       setHorizontalTextPosition(AbstractButton.CENTER);
       
    }
     
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
                
        button = new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), getHeight() / 2, getHeight()/ 2);
        if (rr2d == null) {
            rr2d = new RoundRectangle2DHighlight(button);
        }

        if (ghostValue > 0.0f) {
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            float alphaValue = ghostValue;
            Composite composite = g2.getComposite();
            if (composite instanceof AlphaComposite) {
                alphaValue *= ((AlphaComposite) composite).getAlpha();
            }

            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                    alphaValue));

            g2.drawImage(rr2d.getImage(button), null, 0, 0);
            g2.setComposite(composite);
        }
        

        Paint paint = g2.getPaint();
       

        g2.setPaint(new LinearGradientPaint(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight(), new float[]{0, 1}, new Color[]{new Color(0, 0, 0, 80), new Color(255, 255, 255, 200)}, MultipleGradientPaint.CycleMethod.NO_CYCLE));
        g2.fill(button);
          g2.setPaint(paint);

        FontMetrics fm = getFontMetrics(getFont());
        TextLayout layout = new TextLayout(getText(),
                getFont(),
                g2.getFontRenderContext());
        Rectangle2D bounds = layout.getBounds();

        int x = (int) (getWidth() -
                bounds.getWidth()) / 2;
        //x -= 2;
        int y = (getHeight() -
                fm.getMaxAscent() - fm.getMaxDescent()) / 2;
        y += fm.getAscent() - 1;
        
         
        layout.draw(g2, x, y);
       
    


    }
     private final class HiglightHandler extends MouseAdapter {
        private Animator timer;
        /*
        private Cycle cycle = new Cycle(300, 1000 / 30);
        private Envelope envelope = new Envelope(1.0, 2, RepeatBehavior.FORWARD, EndBehavior.HOLD);
          */      
        
        @Override
        public void mouseEntered(MouseEvent e) {
            if (timer != null && timer.isRunning()) {
                timer.stop();
            }
            timer = new Animator(300, new AnimateGhost(true));
                    
            timer.start();
        }
        
        @Override
        public void mouseExited(MouseEvent e) {
            if (timer != null && timer.isRunning()) {
                timer.stop();
            }
            timer = new Animator(300, new AnimateGhost(false));
            timer.start();
        }
    }
    
    private final class AnimateGhost implements TimingTarget {
        private boolean forward;
        private float oldValue;
        
        
        AnimateGhost(boolean forward) {
            this.forward = forward;
            oldValue = ghostValue;
        }
        
        public void timingEvent(float fraction) {
            ghostValue = oldValue + fraction * (forward ? 1.0f : -1.0f);
            
            if (ghostValue > 1.0f) {
                ghostValue = 1.0f;
            } else if (ghostValue < 0.0f) {
                ghostValue = 0.0f;
            }
            
            repaint();
        }
        
        public void begin() {
        }
        
        public void end() {
        }

      

        public void repeat() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

}
